package com.mall.service.marketing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.SmsFlashPromotionSession;
import com.mall.model.vo.SmsFlashPromotionSessionDetail;
import com.mall.service.marketing.mapper.SmsFlashPromotionProductRelationMapper;
import com.mall.service.marketing.mapper.SmsFlashPromotionSessionMapper;
import com.mall.service.marketing.service.SmsFlashPromotionSessionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class SmsFlashPromotionSessionServiceImpl extends ServiceImpl<SmsFlashPromotionSessionMapper, SmsFlashPromotionSession>
        implements SmsFlashPromotionSessionService {

    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;

    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    /**
     * 获取全部可选场次及其数量
     *
     * @param flashPromotionId
     * @return
     */
    @Override
    public List<SmsFlashPromotionSessionDetail> selectList(Integer flashPromotionId) {
        List<SmsFlashPromotionSession> smsFlashPromotionSessions = smsFlashPromotionSessionMapper.selectList(null);
        if (CollUtil.isEmpty(smsFlashPromotionSessions)) {
            return new ArrayList<>();
        }

        List<SmsFlashPromotionSessionDetail> smsFlashPromotionSessionDetailList = smsFlashPromotionSessions.stream().map(new Function<SmsFlashPromotionSession, SmsFlashPromotionSessionDetail>() {
            @Override
            public SmsFlashPromotionSessionDetail apply(SmsFlashPromotionSession smsFlashPromotionSession) {
                SmsFlashPromotionSessionDetail smsFlashPromotionSessionDetail = new SmsFlashPromotionSessionDetail();
                BeanUtils.copyProperties(smsFlashPromotionSession, smsFlashPromotionSessionDetail);
                //根据flashPromotionId和每个时段的id查询出对应的商品数量
                Integer count = smsFlashPromotionProductRelationMapper.countProductCount(flashPromotionId, smsFlashPromotionSession.getId());
                smsFlashPromotionSessionDetail.setProductCount(count);
                return smsFlashPromotionSessionDetail;
            }
        }).collect(Collectors.toList());
        return smsFlashPromotionSessionDetailList;
    }
}
